package com.fanrui.code.match;

import java.util.Map;

import com.fanrui.code.chess.ChessGamerManager;
import com.fanrui.code.chess.ChessTableManager;
import com.fanrui.code.chess.ITable;
import com.fanrui.code.entity.MahjongTableConfig;
import com.fanrui.code.player.Player;
import com.fanrui.proto.cmd.BaoHuPlayStyle;
import com.fanrui.proto.cmd.FanJing;
import com.fanrui.proto.cmd.GanZhouPlayStyle;
import com.fanrui.proto.cmd.GuiPai;
import com.fanrui.proto.cmd.HuMethod;
import com.fanrui.proto.cmd.MahjongBaseRoomConfig;
import com.fanrui.proto.cmd.MahjongGameType;
import com.fanrui.proto.cmd.MatchGameLevel;
import com.fanrui.proto.cmd.ResEnterGodeGame;
import com.fanrui.proto.cmd.TableType;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;

/**
 * 随机场匹配器
 * @author pc
 *
 */
public class MatchManager {
	// singleton
	private MatchManager() {
	}

	private static class MatchManagerSingleton {
		private static MatchManager instance = new MatchManager();
	}

	public static MatchManager getInstance() {
		return MatchManagerSingleton.instance;
	}
	
	/**
	 * 查找匹配场
	 * @param player
	 * @param gameType
	 * @param gameLevel
	 * @param strategy
	 * @return
	 */
	public synchronized Integer match(Player player, MahjongGameType gameType, MatchGameLevel gameLevel) {
		Integer id = ChessTableManager.getInstance().getCanEnterMatchTable(gameType, gameLevel);
		if(id == null) {
			// 创建房间
			ITable table = ChessTableManager.getInstance().create(getBaseConfigByTypeAndLevel(gameType, gameLevel), gameType, gameLevel);
			if(table != null) {
				return table.getID();
			}
		}
		return id;
	}
	
	/**
	 * 根据麻将类型和等级获取一个数据库房间配置
	 * @param gameType
	 * @param gameLevel
	 * @return
	 */
	private MahjongBaseRoomConfig getBaseConfigByTypeAndLevel(MahjongGameType gameType, MatchGameLevel gameLevel) {
		MahjongTableConfig config = null;
		Map<Integer, MahjongTableConfig> configs = TableConfig.getInstance().getAllMahjongConfigs();
		if(configs != null && !configs.isEmpty()) {
			for (MahjongTableConfig mtc : configs.values()) {
				if(mtc.getGameType() == gameType.getNumber() && mtc.getTableLevel() == gameLevel.getNumber()) {
					config = mtc;
				}
			}
		}
		return toCmd(config);
	}
	
	/**
	 * 数据库麻将房间配置转换成前端需要的数据
	 * @param config
	 * @return
	 */
	private MahjongBaseRoomConfig toCmd(MahjongTableConfig config) {
		MahjongBaseRoomConfig.Builder baseRoomConfig = MahjongBaseRoomConfig.newBuilder();
		baseRoomConfig.setMType(MahjongGameType.forNumber(config.getGameType()));
		baseRoomConfig.setBoardNum(Integer.MAX_VALUE);
		baseRoomConfig.setPlayerNumber(config.getMaxPerson());
		baseRoomConfig.setLocalLimit(config.getLocalLimit());
		baseRoomConfig.setFanJing(FanJing.forNumber(config.getFanJing()));
		baseRoomConfig.setHuType(HuMethod.forNumber(config.getHuMethod()));
		baseRoomConfig.setBanker(config.getZhuang());
		// 赣州冲关玩法
		String ganZhouPlayStyle = config.getGanzhouPlayStyle();
		JsonArray jsonArray = new JsonParser().parse(ganZhouPlayStyle).getAsJsonArray(); //只有一条，以后再改
		if (jsonArray != null && jsonArray.size() > 0) {
			Gson gson = new Gson();
			for (final JsonElement element : jsonArray) {
				GanZhouPlayStyle ele = gson.fromJson(element, GanZhouPlayStyle.class);
				baseRoomConfig.setGanZhouPlayStyle(ele);
			}
		}
		baseRoomConfig.setSameIP(config.getSameIp());
		baseRoomConfig.setSafeDistance(config.getSafeDistance());
		baseRoomConfig.setBuyMa(config.getBuyMa());
		baseRoomConfig.setGuiPai(GuiPai.forNumber(config.getGuipai()));
		// 爆胡玩法
		String baoHuPlayStyle = config.getGanzhouPlayStyle();
		JsonArray jsonArray2 = new JsonParser().parse(baoHuPlayStyle).getAsJsonArray(); //只有一条，以后再改
		if (jsonArray2 != null && jsonArray2.size() > 0) {
			Gson gson = new Gson();
			for (final JsonElement element : jsonArray2) {
				BaoHuPlayStyle ele = gson.fromJson(element, BaoHuPlayStyle.class);
				baseRoomConfig.setBaoHuPlayStyle(ele);
			}
		}
		baseRoomConfig.setEnterLimit(config.getEnterLimit());
		baseRoomConfig.setLeaveLimit(config.getLeaveLimit());
		baseRoomConfig.setTableType(TableType.RandomGold);
		return baseRoomConfig.build();
	}
	
	/**
	 * 是否能进行匹配
	 * 
	 * @param player
	 * @param gameType
	 * @param gameLevel
	 * @return CommonReply.Result
	 */
	public ResEnterGodeGame.Result addMatchConditionCheck(Player player, MahjongGameType gameType, MatchGameLevel gameLevel) {
		if (ChessGamerManager.getInstance().findGamer(player.getId()) != null) {
			return ResEnterGodeGame.Result.Gaming;
		}
		MahjongTableConfig mtc = TableConfig.getInstance().getMahjongTableConfig(gameLevel, gameType);
		if (mtc == null) {
			return ResEnterGodeGame.Result.Failed;
		}

		if (player.getGold() <= mtc.getEnterLimit()) {
			return ResEnterGodeGame.Result.UnGold;
		}
		if (player.getGold() >= mtc.getLeaveLimit()) {
			return ResEnterGodeGame.Result.SurpassLimit;
		}
		return ResEnterGodeGame.Result.Success;
	}

}

